package com.example.utgardtutorial1.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.utgardtutorial1.entity.SimulationSaveNameOne;
import com.example.utgardtutorial1.service.SimulationSaveNameOneService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wwc
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/simulation-save-name-one")
public class SimulationSaveNameOneController {
    @Autowired
    SimulationSaveNameOneService simulationSaveNameOneService;
    @ApiOperation("下载表格")
    @DeleteMapping("/download")
    public void download(String date, HttpServletResponse response) throws IOException {
        QueryWrapper<SimulationSaveNameOne> simulationSaveNameOneQueryWrapper = new QueryWrapper<>();
        simulationSaveNameOneQueryWrapper.eq("date1",date);
        List<SimulationSaveNameOne> simulationSaveNameOnes = simulationSaveNameOneService.list(simulationSaveNameOneQueryWrapper);
        ExcelWriter excelWriter = new ExcelWriter();
        excelWriter.write(simulationSaveNameOnes);
        putContentInExcel(response,excelWriter,date+"数据质量检测报表");
    }

    public void putContentInExcel(HttpServletResponse response, ExcelWriter writer,String fileName) throws IOException {
        CellStyle cellStyle1 = writer.createCellStyle();
        Font font1 = writer.createFont();
        font1.setFontName("宋体");
        font1.setBold(true);
        short shorts = 240;
        font1.setFontHeight(shorts);
        cellStyle1.setFont(font1);
        cellStyle1.setAlignment(HorizontalAlignment.CENTER);
        for (int i = 0;i<=90;i++) {
            writer.setStyle(cellStyle1,i,0);
        }
        response.setHeader("content-disposition","attachment;filename="+new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        writer.close();
        IoUtil.close(out);
        out.close();
    }
}

